﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using Warehouse.API.Applcation.Command.AllocateCommand;
using Warehouse.API.Applcation.Command.EnterStoragesCommand;
using Warehouse.API.Applcation.Command.InjureCommand;
using Warehouse.Domain.DTO;

namespace Warehouse.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class AllocateController : ControllerBase
    {
        protected readonly IMediator mediator;

        public AllocateController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 添加调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddEnterStorage(AddAllocateDto command)
        {
           

            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.AllProducs.FirstOrDefault().office_id = sid;
                command.AllProducs.FirstOrDefault().user_id = Convert.ToInt32(uid);
                result.Code = 200;
                result.Message = "添加调拨管理成功！";
                result.Data = await mediator.Send(new AllocateAddCommand { addDto = command });
           
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 修改调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdEnterStorage(AddAllocateDto command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                command.user_id = Convert.ToInt32(uid);
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                command.office_id = sid;
                command.AllProducs.FirstOrDefault().office_id = sid;
                command.AllProducs.FirstOrDefault().user_id = Convert.ToInt32(uid);
                result.Code = 200;
                result.Message = "修改调拨管理成功！";
                result.Data = await mediator.Send(new AllocateUpdCommand { addDto = command });
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 删除调拨管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult<int>> DelEnterStorage(AllocateDelCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = 200;
                result.Message = "删除调拨管理成功！";
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }



        /// <summary>
        /// 修改调拨审批状态管理
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdEnterSatetStorage(AllocateApprovalCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                result.Code = 200;
                result.Message = "修改调拨管理成功！";
                result.Data = await mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }



    }
}
